package com.example.musicdemo.presenter;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Parcelable;
import android.util.Log;

import com.example.musicdemo.Song;
import com.example.musicdemo.interfaces.IPlayerControl;
import com.example.musicdemo.interfaces.IPlayerViewControl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @类 Jan14
 * @作者: 吴金龙
 * @创建时间: 2022/1/18
 * @描述:
 */
public class PlayerPresenter extends Binder implements IPlayerControl {

    private static final String TAG = "PlayerPresenter";
    private IPlayerViewControl mIPlayerViewControl;
    private int mCurrentState = PLAYER_START_STOP;
    private MediaPlayer mMediaPlayer;
    private Timer mTimer;
    private SeekTimeTask mTimeTask;


    @Override
    public void playOrPause(String dataSource) {
        Log.d(TAG, "playOrPause: ");
        if (mCurrentState == PLAYER_START_STOP) {
            //创建播放器
            initPlayer();
            //初始化数据源
            try {
                mMediaPlayer.setDataSource(dataSource);//放音乐路径
                mMediaPlayer.prepare();//同步播放器准备播放
                mMediaPlayer.start();
                mCurrentState = PLAYER_START_PLAY;
                startTimer();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else if (mCurrentState == PLAYER_START_PLAY) {
            if (mMediaPlayer != null) {
                mMediaPlayer.pause();
                mCurrentState = PLAYER_START_PAUSE;
                stopTimer();
            }
        } else if (mCurrentState == PLAYER_START_PAUSE) {
            if (mMediaPlayer != null) {
                mMediaPlayer.start();
                mCurrentState = PLAYER_START_PLAY;
                startTimer();
            }
        }
        //通知UI界面进行更新
        if (mIPlayerViewControl != null) {
            mIPlayerViewControl.playerStateChange(mCurrentState);
        }
    }

    @Override
    public void playNextMusic(String dataSource) {
        mCurrentState = PLAYER_START_STOP;
        closeMine();
        stopTimer();
        playOrPause(dataSource);
    }

    @Override
    public void playPreMusic(String dataSource) {
        mCurrentState = PLAYER_START_STOP;
        closeMine();
        stopTimer();
        playOrPause(dataSource);
    }

    /**
     * 初始化播放器
     */
    private void initPlayer() {
        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
    }


    public void closeMine() {
        Log.d(TAG, "stop: ");
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
            mCurrentState = PLAYER_START_STOP;
            if (mIPlayerViewControl != null) {
                mIPlayerViewControl.playerStateChange(mCurrentState);
            }
            mMediaPlayer.release();
            mMediaPlayer = null;
        }

    }

    @Override
    public void seekTo(int seek) {
        Log.d(TAG, "seekTo: ");
        if (mMediaPlayer != null) {
            int tarSeek = (int) (seek * 1.0f / 100 * mMediaPlayer.getDuration());
            mMediaPlayer.seekTo(tarSeek);
        }

    }

    @Override
    public void registerViewControl(IPlayerViewControl viewControl) {
        Log.d(TAG, "registerViewControl: ");
        this.mIPlayerViewControl = viewControl;

    }

    @Override
    public void unRegisterViewControl() {
        Log.d(TAG, "unRegisterViewControl: ");

    }

    private class SeekTimeTask extends TimerTask {

        @Override
        public void run() {
            if (mMediaPlayer != null && mIPlayerViewControl != null) {

                int currentPosition = mMediaPlayer.getCurrentPosition();
                //获取当前的播放速度
                // Log.d(TAG, "SeekTimeTask-->currentTime: "+currentPosition);
                int mCurrentPosition = (int) (currentPosition * 1.0f / mMediaPlayer.getDuration() * 100);
                mIPlayerViewControl.seekBarChange(mCurrentPosition);
            }
        }
    }

    private void startTimer() {
        if (mTimer == null) {

            mTimer = new Timer();
        }
        if (mTimeTask == null) {

            mTimeTask = new SeekTimeTask();
        }
        mTimer.schedule(mTimeTask, 0, 500);
    }

    private void stopTimer() {
        if (mTimeTask != null) {
            mTimeTask.cancel();
            mTimeTask = null;
        }
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

}
